package cn.tedu.bank.web;

import cn.tedu.bank.exceptions.MoneyNotEnoughException;
import cn.tedu.bank.exceptions.TransferException;
import cn.tedu.bank.service.AccountService;
import cn.tedu.bank.service.impl.AccountServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 表示层
 */
@WebServlet("/transfer")
public class AccountServlet extends HttpServlet {
    //为了让这个对象在其他方法中也可以用，声明为实例变量
    private AccountService accountService = new AccountServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取表单数据
        String fromActno = request.getParameter("fromActno");
        String toActno = request.getParameter("toActno");
        double money = Double.parseDouble(request.getParameter("money"));
        //Servlet作用是控制器，前端发送请求到Servlet这里，只负责接收数据，接收数据后，调用service的转账方法完成转账（掉业务层，Servlet此处不处理业务）
        try {
            //调用service的转账方法完成转账（掉业务层，Servlet此处不处理业务）
            accountService.transfer(fromActno,toActno,money);
            //程序到这里表示转账一定成功
            //调用view完成展示结果
            // response.sendRedirect(request.getContextPath() + "/页面路径");
            response.sendRedirect(request.getContextPath() + "/success.html");
        } catch (MoneyNotEnoughException e) {
            response.sendRedirect(request.getContextPath() + "/error1.html");
        } catch (TransferException e) {
            response.sendRedirect(request.getContextPath() + "/error2.html");
        }catch (Exception e){
            response.sendRedirect(request.getContextPath() + "/error2.html");
        }
        //调用View完成展示结果

    }
}
